﻿using DataRepositoryLayer.Interfaces;
using ModelLayer;
using ModelLayer.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataRepositoryLayer.Classes
{
    public class RouteReservationRepository : GenericDataRepository<RouteReservation>, IRouteReservationRepository
    {
        public RouteReservationRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {

        }

        public void ReserveRoute(IList<BatteryToReserveDTO> batteriesToReserve, BatteryType type, UserAccount userAccount)
        {
            RouteReservation routeReservation = new RouteReservation() { account_id = userAccount.id }; //remove type of route too, useless
            double maximumDistance = type.maximum_distance;
            foreach (BatteryToReserveDTO dto in batteriesToReserve)
            {
                // Remove ordinal from DB it is useless
                BatteryReservation reservation = new BatteryReservation() { battery_id = dto.BatteryToReserve.id, time_of_reservation = dto.ReservationTime, completed = false };
                reservation.estimated_usage = dto.Distance / maximumDistance;
                routeReservation.BatteryReservations.Add(reservation);
            }
            Add(routeReservation);

        }


        public IList<RouteReservation> GetUserReservations(UserAccount account)
        {
            return GetList(rr => rr.account_id == account.id, br => br.BatteryReservations);
        }
    }
}
